import { reactive } from 'vue'

interface VCodeParam {
  disabled: boolean
  counter: number
  timer: any
}

function useVCode() {
  const VCode = reactive<VCodeParam>({
    disabled: false,
    counter: 60,
    timer: null,
  })

  const setVCode = () => {
    VCode.disabled = true
    VCode.timer = setInterval(() => {
      VCode.counter = VCode.counter - 1
      if (VCode.counter <= 0) {
        VCode.counter = 0
        clearInterval(VCode.timer)
        VCode.disabled = false
        VCode.counter = 60
      }
    }, 1000)
  }

  const resetSendCode = () => {
    Object.assign(VCode, {
      disabled: false,
      counter: 60,
      timer: null,
    })
  }
  return {
    VCode,
    setVCode,
    resetSendCode,
  }
}

export { useVCode }
